package com.starlink.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.hutool.core.convert.Convert;
import com.starlink.common.PageInfoTwo;
import com.starlink.common.utils.AssertUtil;
import com.starlink.model.dtomapper.UserProductCollectDtoMapper;
import com.starlink.model.domain.UserProductCollectDO;
import com.starlink.mapper.UserProductCollectMapper;
import com.starlink.model.request.UserProductCollectRequest;
import com.starlink.model.request.save.UserProductCollectSaveRequest;
import com.starlink.model.util.UserProductCollectConvert;
import com.starlink.service.UserProductCollectService;
import com.starlink.model.vo.UserProductCollectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zhanghong
 */
@Service
public class UserProductCollectServiceImpl extends ServiceImpl<UserProductCollectMapper, UserProductCollectDO> implements UserProductCollectService {
    @Autowired
    private UserProductCollectMapper userProductCollectMapper;

    @Override
    public UserProductCollectVO getByKey(long id) {
        UserProductCollectVO userProductCollectVO = UserProductCollectDtoMapper.INSTANCE.do2VO(getById(id));
        return userProductCollectVO;
    }

    @Override
    public PageInfoTwo<UserProductCollectVO> pageQuery(UserProductCollectRequest request) {
        long count = userProductCollectMapper.pageCount(request);
        List<UserProductCollectDO> list = userProductCollectMapper.pageQuery(request, request.getPageSize(), request.fetchOffSet());
        List<UserProductCollectVO> userProductCollectVOList = UserProductCollectConvert.buildUserProductCollectVoList(list);
        return new PageInfoTwo(request.getPageNum(), request.getPageSize(), userProductCollectVOList, count);
    }

    @Override
    public List<UserProductCollectVO> listQuery(UserProductCollectRequest request) {
        List<UserProductCollectDO> list = userProductCollectMapper.selectDOList(UserProductCollectDtoMapper.INSTANCE.request2DO(request));
        List<UserProductCollectVO> userProductCollectVOS = UserProductCollectConvert.buildUserProductCollectVoList(list);
        return userProductCollectVOS;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int add(UserProductCollectSaveRequest request) {
        AssertUtil.assertNotNull(request.getProductId(), "收藏物品id不能为空");
        // 判断是否已经收藏
        AssertUtil.assertNull(userProductCollectMapper.selectByBookId(request.getProductId(),request.getUserId()), "该商品已经收藏");

        UserProductCollectRequest collectRequest = new UserProductCollectRequest();
        collectRequest.setUserId(request.getUserId());
        collectRequest.setProductId(request.getProductId());
        collectRequest.setPicture(request.getPicture());
        collectRequest.setTitle(request.getTitle());
        collectRequest.setProductType(request.getProductType());
        return userProductCollectMapper.insertDO(UserProductCollectDtoMapper.INSTANCE.request2DO(collectRequest));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByKey(UserProductCollectRequest request) {
        AssertUtil.assertNotNull(request.getId(), "id不能为空");
        saveOrUpdate(UserProductCollectDtoMapper.INSTANCE.request2DO(request));
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteDOByIds(String ids) {
        userProductCollectMapper.deleteDOByIds(Convert.toStrArray(ids));
        return true;
    }

    @Override
    public Long selectByBookId(Long mallBooksId,Long userId) {
        return userProductCollectMapper.selectByBookId(mallBooksId,userId);
    }
}
